/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Client.geass.gui.searchGUI;

import Client.geass.clientController.searchController.CityLogic;
import Client.geass.clientController.searchController.CityLogicInterface;

import Client.geass.gui.travellerGUI.TableModel;

import Shared.geass.dataPOJO.Phase;
import Shared.geass.dataPOJO.Plan;

import java.awt.Point;

import java.util.ArrayList;
import java.util.Date;

import javax.swing.JOptionPane;


import javax.swing.JTextField;
;
import javax.swing.table.DefaultTableModel;


/**
 *
 * @author Kite
 */
public class PlanSearchPanel extends javax.swing.JPanel {

  

   
    /**
     * Creates new form PlanSearchPanel
     */
    public PlanSearchPanel(userPlanPanel up,CityLogicInterface c) {

        initComponents();
        this.up = up;
        setDrag();
        this.c=c;
      

    }

   
  
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        searchTable = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        searchButton1 = new javax.swing.JButton();
        jDateChooser1 = new com.toedter.calendar.JDateChooser();
        jDateChooser2 = new com.toedter.calendar.JDateChooser();
        jDateChooser3 = new com.toedter.calendar.JDateChooser();
        searchButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        comp = new javax.swing.JTextField();

        jScrollPane1.setName("表格"); // NOI18N

        searchTable.setModel(planTable);
        searchTable.setName("search");
        jScrollPane1.setViewportView(searchTable);

        jPanel1.setOpaque(true);

        searchButton1.setText("搜一下");
        searchButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButton1ActionPerformed(evt);
            }
        });

        searchButton2.setText("搜一下");
        searchButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButton2ActionPerformed(evt);
            }
        });

        jLabel1.setText("计划开始时间");

        jLabel2.setText("计划中的某一天");

        jLabel3.setText("计划结束时间");

        jLabel4.setText("城市名称");

        jButton1.setText("查看");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        comp.setEditable(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(16, 16, 16)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jDateChooser1, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
                    .addComponent(jDateChooser3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jDateChooser2, javax.swing.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE)
                    .addComponent(jTextField1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(searchButton1)
                    .addComponent(searchButton2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addComponent(comp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jDateChooser2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jDateChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(searchButton1)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(searchButton2)
                        .addComponent(jLabel4)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jLabel2)
                        .addComponent(jDateChooser3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(comp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 584, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 23, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 412, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        //a拖拽所选的行数限定
    }// </editor-fold>//GEN-END:initComponents
   
   
    int index ;
    private void setDrag() {
        searchTable.addMouseListener(new java.awt.event.MouseAdapter() {
           public void mouseReleased(java.awt.event.MouseEvent evt) {
                     
                 p = evt.getPoint();
              //  System.out.println(evt.getX()+","+evt.getY());
                 if(p.getX()<-70)
                    up.release(plan,getPhase());
                    comp.setVisible(false);
 //到达有效区 填充表格              
                if (valid()) {
                    up.getPlanByuser();
                    
                }
               
            }
        });


        searchTable.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
                
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                 
                // 拖拽 移动
                     p.setLocation(evt.getX()-30, evt.getY()+50);
               
                       GhostPaint(comp);   
                 //拖拽到一定位置       
                      if (valid( )) {
                            up.enter(p,detail);
                          
                         if(p.getX()<-70)
                             up.display();
                          
                   }  
                // 限制拖拽的行数
                  index =searchTable.getSelectedRow();
                  searchTable.setRowSelectionInterval(index,index);

                    }
        });

    }    
               
           
                  
                 
     
// 判断 鼠标是否在有效区域
    public boolean valid() {
        boolean valid = false;
        int x = (int) p.getX();
        int y = (int) p.getY();
        if ((x <= -31) && (x >= -126) && (y < 297) && (y > -30)) {  // 进入 边界 区域 
            valid = true;
        }    
        if((x <= -135) && (x >= -730) && (y < 297) && (y > -30)){  // 进入 目标表格区域
            valid = true;
        }
        return valid;
    }



   

    public ArrayList<Phase> getPhase() {
        return phaselist;//jButton1ActionPerformed 方法中 初始化，可以 重新赋值
    }
     public JTextField getComp(){
         return comp ;
     }
   
public void GhostPaint(JTextField comp){
        // comp.setText());
             plan =planlist.get(index);
          detail = plan.getPlantitle()+"      ，      "+plan.getUsername()+".....";
         comp.setVisible(true);
         comp.setText(detail);
         comp.setSize(200, 25);
         comp.setLocation(p);
         comp.repaint();
       }
 
    private void searchButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButton1ActionPerformed
        // TODO add your handling code here:
        Date start = jDateChooser1.getDate();
        Date end = jDateChooser2.getDate();

        if (start == null || end == null) {
            JOptionPane.showMessageDialog(this, "请选择日期：）");
        } else {

            this.searchByTwoDate(start, end);

        }
    }//GEN-LAST:event_searchButton1ActionPerformed

    private void searchButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButton2ActionPerformed
        // TODO add your handling code here:
        Date date = jDateChooser3.getDate();
        String searchCondition = this.jTextField1.getText();
        if (searchCondition.equals("")) {
            JOptionPane.showMessageDialog(this, "请输入城市名：）");
        } else if (date == null) {
            JOptionPane.showMessageDialog(this, "请输入选择日期：）");
        } else {

            this.searchByCityDate(searchCondition, date);

        }


    }//GEN-LAST:event_searchButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
         int index = searchTable.getSelectedRow();
                int id;

                if ((planlist != null) && (index != -1)) {
                    id = planlist.get(index).getPlanid();
                    // System.out.println(id);
                    phaselist = c.getPhase(id);
                    // System.out.println(phaselist.get(0).getSite());
                    pd.setP(phaselist);
                    pd.setVisible(true);

                }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void searchByTwoDate(Date start, Date end) {

        // planlist = dbcity.searchPlanByTwoDate(start, end);
        planlist = c.searchPlanByTwoDate(start, end);
        if (planlist == null) {
            System.out.println("没有找到符合条件的计划");
            return;
        }
        this.planTable.setDataVector(TableModel.getInstance().planTableVector(planlist), TableModel.PLAN_COLUMN_NAMES);
    }

    private void searchByCityDate(String searchCondition, Date date) {
        // planlist = dbcity.searchPlanByCityDate(searchCondition, date);
        planlist = c.searchPlanByCityDate(searchCondition, date);
        if (planlist == null) {
            System.out.println("没有找到符合的城市");
            return;
        }
        this.planTable.setDataVector(TableModel.getInstance().planTableVector(planlist), TableModel.PLAN_COLUMN_NAMES);
    }
    private userPlanPanel up = null;
    private Point p = new Point(0,0) ;
    private  Plan plan = null;
    private ArrayList<Plan> planlist = null;
    private ArrayList<Phase> phaselist = null;
    private CityLogicInterface c ;
    private  PhaseSearchDialog pd = new PhaseSearchDialog();
    private  DefaultTableModel planTable = new DefaultTableModel(null, TableModel.PLAN_COLUMN_NAMES);
    private String detail;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField comp;
    private javax.swing.JButton jButton1;
    private com.toedter.calendar.JDateChooser jDateChooser1;
    private com.toedter.calendar.JDateChooser jDateChooser2;
    private com.toedter.calendar.JDateChooser jDateChooser3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JButton searchButton1;
    private javax.swing.JButton searchButton2;
    private javax.swing.JTable searchTable;
    // End of variables declaration//GEN-END:variables
}
